// Copyright 2011 The OnTheQuarter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package server

import (
	"fmt"
	"regexp"
	"strconv"
	"time"
)

func newQuarter(year int64, qtr int) Quarter {
	return Quarter(fmt.Sprintf("%dq%d", year, qtr))
}

func currentQuarter() Quarter {
	now := time.UTC()
	return newQuarter(now.Year, now.Month/4 + 1)
}

var quarterRegex = regexp.MustCompile(`^[0-9]+q[1-4]`)

func (q Quarter) valid() bool {
	return quarterRegex.Match([]byte(q))
}

func (q Quarter) Year() int64 {
	y, err := strconv.Atoi64(string(q)[0:4])
	if err != nil {
		panic(err)
	}
	return y
}

func (q Quarter) Quarter() int {
	qtr, err := strconv.Atoi(string(q)[5:])
	if err != nil {
		panic(err)
	}
	return qtr

}
